package atguigu.shijianduanquchongbingleijia;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class Main {

	public static void main(String[] args) {
		// debugData();
		countUseSeconds();
	}

	public static void countUseSeconds() {
		TimerHelper helper = new TimerHelper();
		File source = new File("d:/aaa.json");
		String s;
		try {
			s = FileUtils.readFileToString(source, Charset.forName("GBK"));
			JSONObject jsonObject = JSONObject.parseObject(s);
			JSONArray jsonArray = jsonObject.getJSONArray("量热计R");
			System.out.println(jsonArray.size());
			List<JdlVo> voList = new ArrayList<>();
			for (Object value : jsonArray) {
				JSONObject json = (JSONObject) value;
				JdlVo vo = new JdlVo();
				vo.setStart(json.getString("factTestPlanStartDate"));
				vo.setEnd(json.getString("factTestPlanEndDate"));
				vo.setEquipmentName(json.getString("量热计R"));
				voList.add(vo);
			}

			// 实际设备在昨天使用时长
			Long useHours = 0L; // 实际使用小时数

			List<TimeBucket> timeBucketList = new ArrayList<>();
			for (JdlVo vo : voList) {
				timeBucketList.add(new TimeBucket(vo.getStart(), vo.getEnd()));
			}
			List<TimeBucket> timeBucketList1;
			try {
				timeBucketList1 = helper.rmReptTimePeriod(timeBucketList);
				Long hour = helper.sumTimeBucketList(timeBucketList1);
				useHours += hour;
				System.out.println(" 使用时长：" + hour + "小时");
			} catch (ParseException e1) {
				e1.printStackTrace();
			}
		} catch (IOException e2) {
			e2.printStackTrace();
		}

	}

	public static void debugData() {
		TimerHelper helper = new TimerHelper();
		File source = new File("d:/aaa.json");
		Date day = helper.ofDay(2022, 3, 28);

		try {
			String s = FileUtils.readFileToString(source, Charset.forName("GBK"));
			JSONObject jsonObject = JSONObject.parseObject(s);
			JSONArray jsonArray = jsonObject.getJSONArray("量热计R");
			System.out.println(jsonArray.size());
			List<JdlVo> voList = new ArrayList<>();

			for (Object value : jsonArray) {
				JSONObject json = (JSONObject) value;
				JdlVo vo = new JdlVo();
				vo.setRecordDate(day);
				vo.setStart(json.getString("factTestPlanStartDate"));
				vo.setEnd(json.getString("factTestPlanEndDate"));
				// vo.setAble(json.getString("设备能力"));
				vo.setEquipmentName(json.getString("量热计R"));
				voList.add(vo);
			}
			Map<String, List<JdlVo>> map = voList.stream().collect(Collectors.groupingBy(e -> e.getEquipmentName()));

			// 实际设备在昨天使用时长
			Long useHours = 0L; // 实际使用小时数
			Long theoryTestHours = 0L;// 设备能力
			Integer jiaDongLv = 0; // 稼动率

			for (Map.Entry<String, List<JdlVo>> entry : map.entrySet()) {
				String key = entry.getKey();
				List<TimeBucket> timeBucketList = new ArrayList<TimeBucket>();
				for (JdlVo vo : entry.getValue()) {
					timeBucketList.add(new TimeBucket(vo.getStart(), vo.getEnd()));
				}
				List<TimeBucket> timeBucketList1 = helper.rmReptTimePeriod(timeBucketList);
				Long hour = helper.sumTimeBucketList(timeBucketList1);
				useHours += hour;
				int stHour = 0;
				if (StringUtils.isNotBlank(entry.getValue().get(0).getAble())) {
					try {
						stHour = Integer.parseInt(entry.getValue().get(0).getAble());
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				theoryTestHours += stHour;
				System.out.println("设备：" + entry.getValue().get(0).getEquipmentName() + " 使用时长：" + hour
						+ "小时， 设备能力：" + stHour);
			}

			// 计算稼动率 ( 使用时长 / 理论时长 ) * 100%
			if (useHours != 0 && theoryTestHours != 0) {
				jiaDongLv = BigDecimal.valueOf(((float) useHours / theoryTestHours) * 100).intValue();
				if (jiaDongLv > 100)
					jiaDongLv = BigDecimal.valueOf(100).intValue();
			}
			System.out.println("稼动率：" + jiaDongLv);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
}
